//
//  MineHeaderView.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/8.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "MineHeaderView.h"
#import "EditInformationViewController.h"


@interface MineHeaderItem :UIView
@property(nonatomic, assign)MineHeaderType itemType;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UILabel *numberLabel;
@property(nonatomic, strong)UIView *lineView;

@end


@interface MineHeaderView ()

@property(nonatomic, strong)UIButton *userHeader;
@property(nonatomic, strong)UIView *lineView;
@property(nonatomic, strong)UIButton *userNameLabel;
///姓名下面的简介，可能会有点击事件所以用button
@property(nonatomic, strong)UIButton *introButton;

///关注
@property(nonatomic, strong)MineHeaderItem *attentionItem;
///粉丝
@property(nonatomic, strong)MineHeaderItem *fansItem;
///动态
@property(nonatomic, strong)MineHeaderItem *dynamicItem;

@end


@implementation MineHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.userHeader = [UIButton buttonWithType:UIButtonTypeCustom];
        self.userHeader.backgroundColor = [UIColor xm_colorFromRGB:0x3E98FD];
        self.userHeader.clipsToBounds = YES;
        self.userHeader.layer.cornerRadius = 36.0;
        [self addSubview:self.userHeader];
        
        self.lineView = [[UIView alloc]init];
        [self addSubview:self.lineView];
        
        self.userNameLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        self.userNameLabel.titleLabel.font = [UIFont xm_medium:17.0];
        [self.userNameLabel setTitleColor:[UIColor xm_jetblackColor] forState:UIControlStateNormal];
        [self.userNameLabel addTarget:self action:@selector(userNameLabelClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.userNameLabel];
        
        self.introButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.introButton.titleLabel.font = [UIFont xm_font:13.0];
        [self.introButton setTitleColor:[UIColor xm_colorFromRGB:0x858C96] forState:UIControlStateNormal];
        [self.introButton setTitleColor:[UIColor xm_colorFromRGB:0x858C96] forState:UIControlStateSelected];
        [self.introButton addTarget:self action:@selector(introButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.introButton];
        
        self.attentionItem = [self mineHeaderItemWithType:MineHeaderType_attention];
        self.fansItem      = [self mineHeaderItemWithType:MineHeaderType_fans];
        self.dynamicItem   = [self mineHeaderItemWithType:MineHeaderType_dynamic];

        [self.introButton setTitle:@"点击查看或编辑个人信息" forState:UIControlStateSelected];
        [self.introButton setTitle:@"首次登录送 200 火球币" forState:UIControlStateNormal];
        
        [self reload];


    }
    return self;
}
-(void)reload{
    UserAccount * account = [UserAccountTool account];
    self.introButton.selected = account.isLogin;
    if (account.isLogin) {
        [self.userNameLabel setTitle:account.NikeName forState:UIControlStateNormal];
    }else{
        [self.userNameLabel setTitle:@"未登录" forState:UIControlStateNormal];
        
    }
}
- (void)userNameLabelClick:(UIButton *)sender{
    [[XMTool shared] login:self.viewController loginBlock:nil];

    if (sender.selected) {
        
    }else{
        
    }
}
-(void)introButtonClick:(UIButton *)sender{
    if (sender.selected) {
        EditInformationViewController * editVC = [[EditInformationViewController alloc] init];
        [self.viewController.navigationController pushViewController:editVC animated:YES];
    }
}


-(MineHeaderItem *)mineHeaderItemWithType:(NSInteger)type{
    MineHeaderItem * mineItem = [[MineHeaderItem alloc]init];
    mineItem.itemType = type;
    [self addSubview:mineItem];
    return mineItem;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [self.userHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(72, 72));
        make.top.mas_equalTo(self).offset(5);
        make.left.mas_equalTo(self).offset(25);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userHeader.mas_right).offset(10);
        make.centerY.mas_equalTo(self.userHeader);
        make.size.mas_equalTo(CGSizeMake(100, 5));
    }];
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.lineView);
        make.bottom.mas_equalTo(self.lineView.mas_top);
    }];
    [self.introButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.lineView);
        make.top.mas_equalTo(self.lineView.mas_bottom);
    }];
    CGSize itemSize = CGSizeMake(self.width/3, 50);
    [self.attentionItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(itemSize);
        make.left.bottom.mas_equalTo(self);
    }];
    [self.fansItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(itemSize);
        make.left.mas_equalTo(self.attentionItem.mas_right);
        make.bottom.mas_equalTo(self);
    }];
    [self.dynamicItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(itemSize);
        make.left.mas_equalTo(self.fansItem.mas_right);
        make.bottom.mas_equalTo(self);
    }];
}
-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
}


@end





@implementation MineHeaderItem

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.textColor = [UIColor xm_colorFromRGB:0x999999];
        self.titleLabel.font = [UIFont xm_font:14.0];
        [self addSubview:self.titleLabel];
        
        self.numberLabel = [[UILabel alloc]init];
        self.numberLabel.textColor = [UIColor xm_colorFromRGB:0x333333];
        self.numberLabel.font = [UIFont xm_medium:18.0];
        [self addSubview:self.numberLabel];
        
        self.lineView = [[UIView alloc]init];
        [self addSubview:self.lineView];
        
        self.numberLabel.text = @"0";
    }
    return self;
}
-(void)setItemType:(MineHeaderType)itemType{
    _itemType = itemType;
    
    switch (_itemType) {
        case MineHeaderType_attention:
            self.titleLabel.text = @"关注";
            break;
        case MineHeaderType_fans:
            self.titleLabel.text = @"粉丝";
            break;
        case MineHeaderType_dynamic:
            self.titleLabel.text = @"动态";
            break;
            
        default:
            break;
    }
    
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.right.left.mas_equalTo(self);
        make.height.mas_equalTo(@10);
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.lineView);
        make.bottom.mas_equalTo(self.lineView.mas_top);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.lineView);
        make.top.mas_equalTo(self.lineView.mas_bottom);
    }];
    
}
-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
}


@end


